package jdl.databaseObjects;

import java.util.ArrayList;
import java.util.List;

public class DbCollection<T extends DbObject> {
	private List<T> objects = new ArrayList<T>();

	protected DbCollection() {

	}

	public int getCount() {
		return objects.size();
	}

	public T getObjectByIndex(int index) {
		return objects.get(index);
	}

	public void add(T object) throws ObjectAlreadyExistsException {
		for (T obj : objects) {
			if (obj.getName() == object.getName())
				throw new ObjectAlreadyExistsException();
		}
		objects.add(object);
	}

	@Deprecated
	protected void addObject(T object) {
		objects.add(object);
	}

	protected void removeObject(T object) {
		objects.remove(object);
	}

	protected T getObjectByName(String name) {
		if (name == null || name.isEmpty())
			throw new IllegalArgumentException("name is null or empty.");
		for (T object : objects) {
			if (object.getName().compareToIgnoreCase(name) == 0)
				return object;
		}
		return null;
	}
}
